#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include <openssl/evp.h>


void print_hex(unsigned char *buf, int len)
{
  int i;
  int n;

  for(i=0,n=0;i<len;i++){
    //if(n > 7){
    //  printf("\n");
    //  n = 0;
    //}
    printf("%02x",buf[i]);
    //n++;
  }
  printf("\n");
}

int main(argc, argv)
  int argc;
  char *argv[];
{
  unsigned char pass[1024]={0};  // password to derive in KEY and IV
  unsigned char salt[1024]={0};      // salt (binary)
  int salt_len;                  // salt length in bytes
  int ic;                        // iterative count
  unsigned char result[1024];       // result (binary - 32+16 chars)

  if ( argc != 2 ) {
    fprintf(stderr, "usage: %s <passwd >\n", argv[0]);
    return 1;
  }

  char *salt_in_Hex = "0102030405060708";
  /*Para salting
  hex2byte(salt, salt_in_Hex, strlen(salt_in_Hex));
  salt_len=strlen(salt_in_Hex)/2;   // WARNING: assume it is a even number!
  printf("SALT = ");
  print_hex(salt, strlen(salt));
  */
  ic = 1;

  strcpy(pass, argv[1]);
  printf("pass =[%s]\n", pass);
  //EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, pass, strlen(pass),ic, result, result+32);
  //EVP_BytesToKey(EVP_des_ecb(), EVP_sha1(), NULL, pass, strlen(pass),ic, result, result+32);
  EVP_BytesToKey(EVP_des_ecb(), EVP_md5 (), NULL, pass, strlen(pass),ic, result, result+32);
 //printf("EVP_BytesToKey(\"%s\", \"%s\", %d)=\n", pass, salt, ic);
  print_hex(result, 32);     // Key
  print_hex(result+32, 16);  // IV

  return(0);
}
